package com.gugee.tiktok.common.model.es.challenge;

import com.gugee.tiktok.common.model.mysql.Challenge;
import com.xiguaji.boot.toolkit.JsonUtil;
import lombok.Data;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.StringUtils;

import java.util.List;

@Data
public class ESChallenge {
    private long id;
    private long cid;
    private String chaName;
    private String desc;
    private boolean isCommerce;
    private long userCount;
    private long userGrowth;
    private long viewCount;
    private long viewGrowth;
    private int type;
    private String region;
    private String coverPhoto;
    private String shareUrl;
    private String qrCode;
    private List<ESVideo> topVideos;
    private List<ESWeekGrowth> weekGrowth;
    private List<ESCategory> categories;
    private long createTime;
    private long updateTime;

    @Mapper(componentModel = "spring")
    public interface ESChallengeMapping{

        @Mapping(target = "topVideos", expression = "java(setTopVideos(challenge))")
        @Mapping(target = "weekGrowth", expression = "java(setWeekGrowth(challenge))")
        ESChallenge mappger(Challenge challenge);

        List<ESChallenge> mappger(List<Challenge> challenges);


        default List<ESVideo> setTopVideos(Challenge challenge){
            if(!StringUtils.isEmpty(challenge.getTopVideos())){
                return JsonUtil.jsonParse(List.class,challenge.getTopVideos());
            }
            return null;
        }

        default List<ESWeekGrowth> setWeekGrowth(Challenge challenge){
            if(!StringUtils.isEmpty(challenge.getWeekGrowth())){
                return JsonUtil.jsonParse(List.class,challenge.getWeekGrowth());
            }
            return null;
        }
    }
}
